<?
include_once 'CoreModel.php';
Class promotionmodel extends  CoreModel {
	private $table;
	function __construct() {
		$this -> table = "promotion";
		parent::__construct();
	}

	function getCountPromotion($search) {
		$this -> whereData($search);
		$this -> db -> from($this -> table);
		return $this -> db -> count_all_results();

	}

	public function getPromotionByMatnr($matnr) {
		//select * from promotion_smat;
		$this -> db -> select("*");
		$this -> db -> where("MATNR", $matnr);
		return $this -> Execute("promotion_smat");
	}

	public function getPromotionByPriceType($promoID, $priceType) {

		$priceType = "0" . ($priceType + 1);

		$this -> db -> select("*");
		$this -> db -> where("PROMO_ID", $promoID);
		$this -> db -> where("SALE_TYPE", $priceType);
		$data = $this -> Execute("promotion", true);

		if ($data != false) {

			$sql = "select f.PROMO_ID,m.* from promotion_fmat f left join material_data m
						on m.MATNR = f.MATNR 
						where f.PROMO_ID = '" . $promoID . "'; ";
			$query = $this -> db -> query($sql);
			if ($query -> num_rows() > 0) {
				$data -> promotionList = $query -> result();
			} else {
				$data = false;
			}

		} else {

		}
		return $data;
	}

	function getPromotionLevel($promotionID) {
		$this -> db -> select("*");
		$this -> db -> where("PROMO_ID", $promotionID);
		$this -> db -> order_by("PROMO_LEVEL", "DESC");
		return $this -> Execute("promotion_level");
	}

	function getPromotion($start, $limit, $search) {
		$this -> db -> select("*");
		$this -> whereData($search);
		$this -> db -> limit($limit, $start);
		$query = $this -> db -> get($this -> table);

		return $query -> result();
	}

	function whereData($search) {

		if (empty($search)) {
			return;
		}
		
		foreach ($search as $in => $val) {
			if (isset($search[$in]) ) {
				if($in!="fromDate" && $in!="endDate"){
					$this -> db -> where($in, $val);
				}
			}
		}
		
		
		if (isset($search['fromDate'])) {
			if (!isset($search['endDate']) || $search['endDate'] == "") {
				$this -> db -> where("START_DATE >  ", $search['fromDate'], null);
				$this -> db -> where("END_DATE <=", '9999-12-31', null);
			} else {
				$this -> db -> where("START_DATE > ", $search['fromDate'], null);
				$this -> db -> where("END_DATE < ", $search['endDate'], null);
			}
		}

	}

	public function getPromotionSmatCount($PromoID) {
		//$custID = ltrim($custID,'0');
		$this -> db -> select("PROMO_ID");
		$this -> db -> where("PROMO_ID", $PromoID);

		$data = $this -> Execute("promotion_smat");
		return count($data);
	}

	public function getPromotionSmat($PromoID, $start, $limit) {
		$this -> db -> select("promotion_smat.MATNR,MAKTX");
		$this -> db -> from("promotion_smat");
		$this -> db -> join("material_data", "promotion_smat.MATNR = material_data.MATNR");
		$this -> db -> where("PROMO_ID", $PromoID);
		$this -> db -> limit($limit, $start);
		$query = $this -> db -> get();
		return $query -> result();

	}

	public function getPromotionFmatCount($PromoID) {
		//$custID = ltrim($custID,'0');
		$this -> db -> select("PROMO_ID");
		$this -> db -> where("PROMO_ID", $PromoID);

		$data = $this -> Execute("promotion_fmat");
		return count($data);
	}

	public function getPromotionFmat($PromoID, $start, $limit) {
		$this -> db -> select("promotion_fmat.MATNR,MAKTX");
		$this -> db -> from("promotion_fmat");
		$this -> db -> join("material_data", "promotion_fmat.MATNR = material_data.MATNR");
		$this -> db -> where("PROMO_ID", $PromoID);
		$this -> db -> limit($limit, $start);
		$query = $this -> db -> get();
		return $query -> result();

	}

	public function getPromoLevelCount($PromoID) {
		//$custID = ltrim($custID,'0');
		$this -> db -> select("PROMO_ID");
		$this -> db -> where("PROMO_ID", $PromoID);

		$data = $this -> Execute("promotion_level");
		return count($data);
	}

	public function getPromoLevel($PromoID, $start, $limit) {
		$this -> db -> select("*");
		$this -> db -> from("promotion_level");
		$this -> db -> where("PROMO_ID", $PromoID);
		$this -> db -> limit($limit, $start);
		$query = $this -> db -> get();
		return $query -> result();

	}

	public function getSmat($PromoID) {

		$this -> db -> select("*");
		$this -> db -> where("PROMO_ID", $PromoID);
		$data = $this -> Execute("promotion_smat", true);
		if ($data != false) {
			$this -> db -> select("promotion_smat.MATNR,MAKTX");
			$this -> db -> join("material_data", "promotion_smat.MATNR = material_data.MATNR");
			$this -> db -> where("PROMO_ID", $PromoID);
			$data -> smatDetail = $this -> Execute("promotion_smat");
		}
		return $data;
	}

	public function getFmat($PromoID) {

		$this -> db -> select("*");
		$this -> db -> where("PROMO_ID", $PromoID);
		$data = $this -> Execute("promotion_fmat", true);
		if ($data != false) {
			$this -> db -> select("promotion_fmat.MATNR,MAKTX");
			$this -> db -> join("material_data", "promotion_fmat.MATNR = material_data.MATNR");
			$this -> db -> where("PROMO_ID", $PromoID);
			$data -> fmatDetail = $this -> Execute("promotion_fmat");
		}
		return $data;
	}

	public function getPLevel($PromoID) {

		$this -> db -> select("*");
		$this -> db -> where("PROMO_ID", $PromoID);
		$data = $this -> Execute("promotion_level", true);
		if ($data != false) {
			$this -> db -> select("*");
			$this -> db -> where("PROMO_ID", $PromoID);
			$data -> promolevelDetail = $this -> Execute("promotion_level");
		}
		return $data;
	}

}
?>